package com.asking.recommend.controller;

import com.asking.domain.History;
import com.asking.domain.Question;
import com.asking.domain.User;
import com.asking.pojo.EsQuestion;
import com.asking.service.QuestionService;
import com.asking.service.UserService;
import com.asking.service.recommended.RecommendService;
import com.asking.utils.JWTUtil;
import com.asking.vo.Result;
import com.asking.vo.ResultVo;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Resource
    private RecommendService recommendService;
    @Resource
    private QuestionService questionService;
    @Resource
    private UserService userService;

    /**
     * 根据用户观看记录推荐
     *
     * @param request
     * @return
     */
    @GetMapping("/byUserIdRecommend")
    public ResponseEntity<Result> byUserIdRecommend(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            token = token.substring(7);
            String user = JWTUtil.getUser(token);
            String[] split = user.split("#");
            List<History> histories = recommendService.categoryHistory(split[1]);
            List<RecommendedItem> his = recommendService.history(1, 2355, 1);
            List<EsQuestion> questions = new ArrayList<>();
            for (History history : histories) {
                for (RecommendedItem recommendedItem : his) {
                    EsQuestion question = questionService.selectByAnswer((int) (recommendedItem.getItemID()));
                    questions.add(question);
                }
            }
            return new ResponseEntity<>(new Result(200, "推荐成功", questions), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(new Result(200, "推荐失败"), HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * 根据分类推荐
     *
     * @param cname
     * @return
     */
    @GetMapping("/byCategory")
    public ResponseEntity<Result> byCategory(String cname) {
        try {
            List<EsQuestion> questions = recommendService.byCategory(cname);
            return new ResponseEntity<>(new Result(200, "分类推荐成功", questions), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500, "分类推荐失败"), HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


 /*   @GetMapping("/byUserHobby")
    public ResponseEntity<Result> byUserHobby(Integer userId) {
        try {
            User user = userService.selectByUser(userId);
            String hobbies = user.getHobby();
            String[] split = hobbies.split(",");
            List<EsQuestion> list = new ArrayList<>();
            for (String hobby : split) {
                List<EsQuestion> esQuestions = recommendService.byUserHobby(hobby);
                for (EsQuestion esQuestion : esQuestions) {
                    list.add(esQuestion);
                }
            }
            return new ResponseEntity<>(new Result(200, "喜好推荐成功", list), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500, "喜好推荐失败"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }*/

    /**
     * 推荐最热问题
     *
     * @return
     */
    @GetMapping("/hot")
    public ResponseEntity<Result> questionHot(Integer page, Integer rows) {
        try {
            ResultVo resultVo = recommendService.questionHot(page, rows);
            return new ResponseEntity<>(new Result(200,"最热",resultVo), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return    new ResponseEntity<>(new Result(200,"失败"),HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}
